package suzf.top.goodscenter.ui.fragment

import android.graphics.drawable.AnimationDrawable
import android.os.Bundle
import android.support.v7.widget.GridLayoutManager
import android.support.v7.widget.LinearLayoutManager
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.kennyc.view.MultiStateView
import com.kotlin.base.ui.adapter.BaseRecyclerViewAdapter
import kotlinx.android.synthetic.main.fragment_category.*
import org.jetbrains.anko.find
import org.jetbrains.anko.support.v4.startActivity
import rx.Observable
import suzf.top.base.ext.setVisible
import suzf.top.base.ext.startLoading
import suzf.top.base.ui.fragment.BaseMvpFragment
import suzf.top.goodscenter.R
import suzf.top.goodscenter.common.GoodsConstant
import suzf.top.goodscenter.data.protocol.Category
import suzf.top.goodscenter.injection.component.DaggerCategoryComponent
import suzf.top.goodscenter.injection.module.CategoryModule
import suzf.top.goodscenter.presenter.CategoryPresenter
import suzf.top.goodscenter.presenter.view.CategoryView
import suzf.top.goodscenter.ui.activity.GoodsActivity
import suzf.top.goodscenter.ui.adapter.SecondCategoryAdapter
import suzf.top.goodscenter.ui.adapter.TopCategoryAdapter
import java.util.concurrent.TimeUnit

/**
 *
 *========================
 * Created with AndroidStudio
 * Author:Mr.Su
 * Date:2019-09-24
 * Time:13:56
 * ========================
 */
class CategoryFragment: BaseMvpFragment<CategoryPresenter>(),CategoryView {

    private lateinit var topAdapter: TopCategoryAdapter
    private lateinit var secondAdapter: SecondCategoryAdapter

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return inflater.inflate(R.layout.fragment_category,container,false)
    }

    override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        initView()
        loadDate()
    }

    /**
     * Dagger注入
     */
    override fun injectComponent() {
        DaggerCategoryComponent.builder()
                .activityComponent(activityComponent)
                .categoryModule(CategoryModule())
                .build()
                .inject(this)
        mPresenter.mView = this
    }
    private fun initView() {
        //  一级菜单
        mTopCategoryRv.layoutManager = LinearLayoutManager(context)
        topAdapter = TopCategoryAdapter(context)
        mTopCategoryRv.adapter = topAdapter
        topAdapter.setOnItemClickListener(object: BaseRecyclerViewAdapter.OnItemClickListener<Category>{
            override fun onItemClick(item: Category, position: Int) {
                for (category in topAdapter.dataList) {
                    category.isSelected = item.id == category.id
                }
                topAdapter.notifyDataSetChanged()

                loadDate(item.id)
            }
        })

        //  二级菜单
        mSecondCategoryRv.layoutManager = GridLayoutManager(context,3)
        secondAdapter = SecondCategoryAdapter(context)
        mSecondCategoryRv.adapter = secondAdapter
        secondAdapter.setOnItemClickListener(object: BaseRecyclerViewAdapter.OnItemClickListener<Category>{
            override fun onItemClick(item: Category, position: Int) {
                startActivity<GoodsActivity>(GoodsConstant.KEY_CATEGORY_ID to item.id)
            }
        })
    }

    private fun loadDate(parentId: Int = 0) {
        if (parentId != 0) {
            mMultiStateView.startLoading()
        }
        Observable.timer(2,TimeUnit.SECONDS).subscribe {
            mPresenter.getCategory(parentId)
        }

    }

    override fun onGetCategoryResult(result: MutableList<Category>?) {

        if (result != null && result.size > 0) {
            if (result[0].parentId ==0) {
                result[0].isSelected = true
                topAdapter.setData(result)
                mPresenter.getCategory(result[0].id)
            }else {
                secondAdapter.setData(result)
                mTopCategoryIv.setVisible(true)
                mCategoryTitleTv.setVisible(true)
                mMultiStateView.viewState = MultiStateView.VIEW_STATE_CONTENT
            }
        }else {
            mTopCategoryIv.setVisible(false)
            mCategoryTitleTv.setVisible(false)
            mMultiStateView.viewState = MultiStateView.VIEW_STATE_EMPTY
        }

    }

}